package com.zx.my.controller;

import cn.hutool.core.date.DateUtil;
import com.zx.account.entity.User;
import com.zx.account.model.ResponseDto;
import com.zx.my.entity.MyStudy;
import com.zx.my.model.StudyChartDto;
import com.zx.my.service.MyStudyService;
import com.zx.util.Constants;
import com.zx.util.SessionDataUtil;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/my")
public class MyStudyController {

    @Autowired
    private MyStudyService myStudyService;

    @RequestMapping("/studyView")
    public String studyView(Model model) {
        User user = SessionDataUtil.getUserInfo();
        List<MyStudy> studies = this.myStudyService.getByUserId(user.getId());
        model.addAttribute("studies", studies);
        return "/my/studyView";
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResponseDto<MyStudy> save(Model model, String studyContent) {
        User user = SessionDataUtil.getUserInfo();
        MyStudy myStudy = new MyStudy()
                .setStudyDate(new Date()).setStudyTime(0)
                .setBeginTime(new Date()).setStudyContent(studyContent).
                        setStudyStatus(Constants.MY_STUDY_RUN).setUserId(user.getId());
        boolean b = this.myStudyService.save(myStudy);
        if (b) {
            return ResponseDto.ok(myStudy);
        } else {
            return ResponseDto.no(null);
        }
    }

    @RequestMapping("/status")
    @ResponseBody
    public ResponseDto<MyStudy> status(Model model) {
        User user = SessionDataUtil.getUserInfo();
        List<MyStudy> list = this.myStudyService.getRunByUserId(user.getId());
        // 2. 没有任务
        if (CollectionUtils.isEmpty(list)) {
            return new ResponseDto<MyStudy>().setCode(2);
            // 1. 有正在进行的任务
        } else if (list.size() == 1) {
            return new ResponseDto<MyStudy>().setCode(1).setData(list.get(0));
            // 3. 异常
        } else {
            return new ResponseDto<MyStudy>().setCode(3);
        }
    }

    @RequestMapping("/finish")
    @ResponseBody
    public ResponseDto<MyStudy> finish(Integer studyId) {
        boolean b = this.myStudyService.finish(studyId);
        if (b) {
            return ResponseDto.ok(null);
        } else {
            return ResponseDto.no(null);
        }
    }

    @RequestMapping("/getChart")
    @ResponseBody
    public ResponseDto<List<StudyChartDto>> getChart() {
        Date end = new Date();
        Date begin = DateUtil.offsetDay(end, -30);
        List<StudyChartDto> chart = this.myStudyService.getChart(begin, end, 1);

        if (chart != null) {
            return ResponseDto.ok(chart);
        } else {
            return ResponseDto.no(null);
        }
    }

}
